//
//  TaiZhangDetailController.m
//  YunGui
//
//  Created by User on 16/4/15.
//  Copyright © 2016年 hanen. All rights reserved.
//

#import "TaiZhangDetailController.h"
#import "UIView+Extension.h"
#import "TaizhangDetailModel.h"

#define labelHeight  [UIView getHeight:25.0f]

@interface TaiZhangDetailController ()
{
    NSArray       *_titleArray;
    NSArray       *_downTitleArray;
    UIScrollView  *_scrollView;
    
    UIView  *_mainView;
    CGFloat   space;
    TaizhangDetailModel *detailModel;
    NSDictionary *dataDict ;
    
}
@end

@implementation TaiZhangDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
   
   self.navigationItem.leftBarButtonItem=[ViewTool getBackBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
    space = [UIView getHeight:10.0f];
    [ViewTool setNavigationBar:self.navigationController.navigationBar];
    self.navigationItem.title = @"台账详情";
    _titleArray = @[@"区域",@"办事处",@"物业",@"小区名称",@"网点名称",@"网点地址",@"经办人",@"经办人电话",@"预估快递量",@"小区户数",@"主柜数",@"副柜数"];
    _downTitleArray = @[@"合同审批单号",@"合同发起时间",@"合同开始时间",@"合同结束时间",@"综合管理费用/年",@"合同年限",@"是否含电费",@"电费情况说明量",@"是否可以做广告",@"广告比例成分",@"是否排他",@"是否与业委会签署",@"付款状态",@"安装位置描述"];
 
    [self creatUI];
    [self initDetailData];
}
//自定义的leftBarButton
- (UIBarButtonItem *)changeBarButtonWithImageName:(NSString *)imageName
{
    UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    [btn setBackgroundImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    UIBarButtonItem *barItem = [[UIBarButtonItem alloc] initWithCustomView:btn];
    return barItem;
}
- (void)creatUI
{
    _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 64)];
    _scrollView.backgroundColor = GrayBackColor;
    _scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:_scrollView];
    
    _mainView = [[UIView alloc] initWithFrame:CGRectMake(0, 15, kScreenWidth, kScreenHeight)];
    _mainView.backgroundColor = [UIColor whiteColor];
    [_scrollView addSubview:_mainView];
    
    UIImageView  *iamgeV = [[UIImageView alloc] initWithFrame:CGRectMake(2.5  *space, 1.5 *space, 20.0f, 20.0f)];
  
    iamgeV.image = [UIImage imageNamed:@"Btn_Normal_Xinxi"];
    [_mainView addSubview:iamgeV];
    
    UILabel *titlelabel= [ViewTool getLabelWith:CGRectMake(iamgeV.maxX +  space, iamgeV.y, 120, 20.0f) WithTitle:@"基本信息" WithFontSize:16.0f WithTitleColor:BLACKTITLECOLOR WithTextAlignment:NSTextAlignmentLeft];
    [_mainView addSubview:titlelabel];
    UIView *line1 =[ViewTool getLineViewWith:CGRectMake(titlelabel.x, titlelabel.maxY +  space, kScreenWidth - titlelabel.x, 1) withBackgroudColor:GrayLineColor];
    [_mainView addSubview:line1];
    
    for (int i = 0; i < _titleArray.count;i ++) {
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(line1.x, line1.maxY + 0.1*space +  labelHeight * i, 100.0f, labelHeight)];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:14.0f];
        label.text = _titleArray[i];
        label.textColor = GRAYCOLOR2;
        [_mainView addSubview:label];
        
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(label.maxX + space, label.y, kScreenWidth - label.maxX - 2 *space, labelHeight)];
        contentLabel.tag = 300 + i;
        contentLabel.textAlignment = NSTextAlignmentLeft;
        contentLabel.text = @"";
        contentLabel.textColor = BLACKTITLECOLOR;
        contentLabel.font = [UIFont systemFontOfSize:15.0f];
        [_mainView addSubview:contentLabel];
    
    }
    
    UILabel *titlelabel2= [ViewTool getLabelWith:CGRectMake(iamgeV.maxX +  space, 0.5*space + line1.maxY+_titleArray.count*labelHeight, 120, 20.0f) WithTitle:@"合同信息" WithFontSize:16.0f WithTitleColor:BLACKTITLECOLOR WithTextAlignment:NSTextAlignmentLeft];
    [_mainView addSubview:titlelabel2];
    UIView *line2 =[ViewTool getLineViewWith:CGRectMake(titlelabel2.x, titlelabel2.maxY + space, kScreenWidth - titlelabel2.x, 1) withBackgroudColor:GrayLineColor];
    [_mainView addSubview:line2];

    for (int i = 0; i < _downTitleArray.count;i ++) {
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(line2.x, line2.maxY + 0.5*space +  labelHeight * i, 120.0f, labelHeight)];
        label.textAlignment = NSTextAlignmentLeft;
        label.font = [UIFont systemFontOfSize:14.0f];
        label.text = _downTitleArray[i];
        label.textColor = GRAYCOLOR2;
        [_mainView addSubview:label];
        
        UILabel *contentLabel = [[UILabel alloc] initWithFrame:CGRectMake(label.maxX + space, label.y, kScreenWidth - label.maxX - 2 *space, labelHeight)];
        contentLabel.tag = 400 + i;
        contentLabel.textAlignment = NSTextAlignmentLeft;
        contentLabel.text = @"";
        contentLabel.textColor = BLACKTITLECOLOR;
        contentLabel.font = [UIFont systemFontOfSize:15.0f];
        [_mainView addSubview:contentLabel];
    }

    _mainView.frame = CGRectMake(0, 15, kScreenWidth, line2.maxY+_downTitleArray.count*(labelHeight+space));
    _scrollView.contentSize = CGSizeMake(0, line2.maxY+_downTitleArray.count*(labelHeight+space));
   
    UIView *linetop =[ViewTool getLineViewWith:CGRectMake(0, 0, kScreenWidth, 1) withBackgroudColor:GrayLineColor];
    [_mainView addSubview:linetop];
    _scrollView.contentSize = CGSizeMake(0, _mainView.maxY + space);
}

- (void)initDetailData
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:@{@"method":@"app.ledger.detail",@"ledger_id":self.districtID}];
    NSDictionary *paramesDict = [DataTool addSignToDic:dict];
    
    [DataTool postWithUrl:HEAD_URL parameters:paramesDict success:^(id data) {
        id backData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
        NSString *status = [(NSDictionary *)backData objectForKey:@"status"];
        
        if ([status isEqualToString:@"SUCCESS"]) {
            dataDict = backData[@"data"];
            NSDictionary *info = [(NSDictionary *)backData objectForKey:@"data"];
            detailModel = [[TaizhangDetailModel alloc] init];
            [detailModel setValuesForKeysWithDictionary:info];
            [self useData];
        } else {
            NSString *msg = [(NSDictionary *)backData objectForKey:@"msg"];
            [SVProgressHUD showErrorWithStatus:msg];
        }
    } fail:^(NSError *error) {
        [SVProgressHUD showErrorWithStatus:error.localizedDescription];
    }];
}

- (void)useData
{    
    for (int i = 0; i<_titleArray.count; i++) {
        UILabel *label = [_mainView viewWithTag:300+i];
        if (i == 0) {
            //NSString *str = detailModel.area;
            label.text = detailModel.area;
        }
        if (i == 1){
            label.text = detailModel.agency;
        }
        if (i == 2){
            label.text = detailModel.property;
        }
        if (i == 3){
            label.text = detailModel.complex;
        }
        if (i == 4){
            label.text = detailModel.branch_name;
        }
        if (i == 5){
            label.text = detailModel.address;
        }
        if (i == 6){
            label.text = detailModel.dealer;
        }
        if (i == 7){
            label.text = detailModel.dealer_tel;
        }
        if (i == 8){
            label.text = detailModel.package_num;
        }
        if (i == 9){
            label.text = [NSString stringWithFormat:@"%@",detailModel.complex_total];
        }
        if (i == 10){
            label.text = detailModel.main_qty;
        }
        if (i == 11){
            label.text = detailModel.slave_qty;
        }
    }
    for (int i = 0; i<_downTitleArray.count; i++) {
        
        UILabel *label = [_mainView viewWithTag:400 + i];
        if (i == 0) {
            label.text = detailModel.approval_no;
        }
        if (i == 1){
            label.text = detailModel.launch_at;
        }
        if (i == 2){
            label.text = detailModel.begin_on;
        }
        if (i == 3){
            label.text = detailModel.end_on;
        }
        if (i == 4){
            label.text = detailModel.annual_price;
        }
        if (i == 5){
            label.text = detailModel.duration;
        }
        if (i == 6){
            if([detailModel.contain_power intValue] == 1){
                label.text = @"是";
            }else{
                label.text = @"否";
            }
        }
        if (i == 7){
            label.text = detailModel.contain_power_desc;
        }
        if (i == 8){
            label.text = detailModel.is_ads;
        }
        
        if (i == 9){
            label.text = detailModel.ad_ratio;
        }
        if (i == 10){
            if([detailModel.is_exclusive intValue] == 1){
                label.text = @"是";
            }else{
                label.text = @"否";
            }
        }
        if (i == 11){
            if([detailModel.is_committee intValue] == 1){
                label.text = @"是";
            }else{
                label.text = @"否";
            }        }
        if (i == 12){
            if([detailModel.pay_status intValue] == 2){
                label.text = @"待确认";
            }else if([detailModel.pay_status intValue] == 1){
                label.text = @"已付";
            }else{
                label.text = @"未付";
            }
        }
        if (i == 13){
            label.text = detailModel.position_desc;
        }
   }
}

- (void)goToBack{
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end